//<script>
FILE_URL = '<?=URL?>';
FILE_KEY = '<?=$agentkey?>';
FILE_optid = '<?=$optid?>';
FILE_optname = '<?=$optname?>';

/**
*	上传头像
*/
js.updateface=function(na,fid,fun,yt){
	var val = '';
	if(fid){if(get(fid) && get(fid).value)val=get(fid).value;
	if(!val && get(fid+'_img'))val=get(fid+'_img').src;}
	if(!val)val=''+FILE_URL+'images/noimg.jpg';
	var qzui = '{FILEURL}',radius='0px';
	val = val.replace(qzui, FILE_URL);
	if(NOWURL==FILE_URL)qzui='';
	if(get(fid+'_img'))radius = get(fid+'_img').style['border-radius'];
	js.tanbody('updateface',na,300,200,{
		html:'<div align="center" style="padding:10px"><img id="updatefaceimg" onclick="js.viewimg(this)" src="'+val+'" height="110px" width="110px" style="border-radius:'+radius+'"><div><form name="form_updateface"><input type="file" id="updateface" style="width:120px"></form></div></div>',
		btn:[{text:'确定'}]
	});
	var xuanimg = '',bdata={},xuanimg_url='',msgid='msgview_updateface';
	$('#updateface_btn0').click(function(){
		if(xuanimg){
			if(fid){
				if(get(fid))get(fid).value=xuanimg;
				if(get(fid+'_img'))get(fid+'_img').src=xuanimg_url;
			}
			if(fun)fun(xuanimg, bdata);
		}
		js.tanclose('updateface');
	});
	var onsuccess=function(f,d){
		bdata = d;
		if(d.thumbpathout)d.thumbpath = d.thumbpathout;
		if(d.filepathout)d.filepath = d.filepathout;
		var lujs = d.thumbpath;
		if(lujs){
			if(yt)lujs = d.filepath;
			if(lujs.substr(0,4)!='http'){
				xuanimg_url = FILE_URL+lujs;
			}else{
				xuanimg_url = lujs;
				qzui='';
			}
			get('updatefaceimg').src = xuanimg_url;
			if(d.autoup_thumb){
				qzui = '';
				lujs = d.autoup_thumb;
				if(yt && d.autoup_path)lujs = d.autoup_path;
			}
			xuanimg = qzui+lujs;
		}
		js.setmsg('','',msgid);
	}
	var inidofu=function(){
		$.rockfileupload({
			initremove:false,inputfile:'updateface',uptype:'image',
			onsuccess:onsuccess,
			onupstart:function(){js.setmsg('上传中...','',msgid);},
			onprogress:function(f,per){js.setmsg('上传中('+per+'%)...','',msgid);},
			onerror:function(ts){js.setmsg(ts,'',msgid);}
		});
	}
	if(!$.rockfileupload){
		js.importjs(''+FILE_URL+'?m=filejs&key='+FILE_KEY+'',function(){inidofu();});
	}else{
		inidofu();
	}
}

js.updateclear= function(fid){
	if(get(fid))get(fid).value='';
	if(get(fid+'_img'))get(fid+'_img').src=''+FILE_URL+'images/noimg.jpg';
}

js.fileuppath = function(lj){
	if(lj)lj = lj.replace('{FILEURL}', FILE_URL);
	return lj;
}

js.uploadfila = {};
js.uploadfile = function(fid,lx,ulx,cds,cans){
	if(!cds)get(''+fid+'_changeid').click();
	if(!$.rockfileupload){
		js.importjs(''+FILE_URL+'?m=filejs&key='+FILE_KEY+'',function(){js.uploadfile(fid,lx,ulx,true,cans)});
		return false;
	}
	if(!this.uploadfila[lx])this.uploadfila[lx]  = {};
	var oldids = '';
	if(get(fid+'_id'))oldids = get(fid+'_id').value;
	if(!this.uploadfila[lx][fid])this.uploadfila[lx][fid]= $.rockfileupload({
		autoup:false,fileidinput:fid+'_id',fid:fid,
		inputlx:lx,oldids:oldids,uptype:ulx,
		fileview:''+fid+'_view',
		allsuccess:function(da,ids){
			js.uploadfila[this.inputlx][this.fid].allok = true;
			js.uploadstart(this.inputlx, js._temxuy+1);
		},
		onsuccess:function(f,d){
			$('#uploings').html(''+f.filename+'上传成功');
		},
		onprogress:function(f,pev){
			$('#uploings').html(''+f.filename+'上传中('+pev+'%)...');
		},
		onupstart:function(f){
			var str = ''+f.filename+'上传中(0%)...';
			if(!get('uploings'))js.loading('<span id="uploings">'+str+'</span>');
			$('#uploings').html(str);
		}
	});
	if(!cans)cans={};
	cans.uptype = ulx;
	cans.fid 	= fid;
	cans.inputlx= lx;
	cans.oldids = oldids;
	var bose = this.uploadfila[lx][fid];
	bose.setcans(cans);
	return bose;
}

js.uploadchange=function(o,fid,lx){
	this.uploadfila[lx][fid].change(o);
}

/**
*	开始上传,xu=-1
*/
js.uploadstart=function(lx,xu, usd){
	var fid,arr = this.uploadfila[lx];
	this._temxuy= xu;
	if(xu==-1){
		this.ulstses = [];
		this._temxfun= usd;
		for(fid in arr)this.ulstses.push(fid);
		js.uploadstart(lx, 0);
	}else{
		if(xu>=this.ulstses.length){
			js.unloading();
			this._temxfun();
		}else{
			arr[this.ulstses[xu]].start();
		}
	}
}

js.fileall=',aac,ace,ai,ain,amr,app,arj,asf,asp,aspx,av,avi,bin,bmp,cab,cad,cat,cdr,chm,com,css,cur,dat,db,dll,dmv,doc,docx,dot,dps,dpt,dwg,dxf,emf,eps,et,ett,exe,fla,ftp,gif,hlp,htm,html,icl,ico,img,inf,ini,iso,jpeg,jpg,js,m3u,max,mdb,mde,mht,mid,midi,mov,mp3,mp4,mpeg,mpg,msi,nrg,ocx,ogg,ogm,pdf,php,png,pot,ppt,pptx,psd,pub,qt,ra,ram,rar,rm,rmvb,rtf,swf,tar,tif,tiff,txt,url,vbs,vsd,vss,vst,wav,wave,wm,wma,wmd,wmf,wmv,wps,wpt,wz,xls,xlsx,xlt,xml,zip,';
js.filelxext = function(lx){
	if(js.fileall.indexOf(','+lx+',')<0)lx='wz';
	return lx;
}
js.fileshowarr = {};
js.fileshowview=function(fid, farr, ised){
	if(!farr)return;
	var s='',i,src,clds='',d;
	for(i=0;i<farr.length;i++){
		d = farr[i];
		this.fileshowarr[d.id] = d;
		clds= '';
		src = ''+FILE_URL+'images/fileicons/'+js.filelxext(d.fileext)+'.gif';
		if(d.thumbpath){src=js.fileuppath(d.thumbpath);clds='js.viewimg(this)';}
		d.src = src;
		this.fileshowarr[d.id] = d;
		s+='<div class="upload_items" id="'+fid+'show_'+d.id+'" onclick="js.fileshowclick('+d.id+',\''+fid+'\')">';
		if(d.thumbpath){
			s+='<img class="imgs" src="'+src+'">'
		}else{
			s+='<div class="upload_items_items"><img src="'+src+'" align="absmiddle"> ('+d.filesizecn+')<br><font>'+d.filename+'</font></div>';
		}
		s+='</div>';
	}
	$('#'+fid+'_view').prepend(s);
}

js.fileshowdel=function(o1,id1, fid, ob){
	if(!ob){
		js.confirm('确定要删除此文件吗？', function(jg){
			if(jg=='yes')js.fileshowdel(o1,id1, fid, true);
		});
	}else{
		$('#'+fid+'show_'+id1+'').remove();
		$.get('api.php?m=file&a=del&id='+id1+'');
		var o2 = get(''+fid+'_id');if(!o2)return;
		var val = o2.value,st2;
		if(val){
			var vala = val.split(','),dva=[];
			for(var i=0;i<vala.length;i++)if(vala[i]!=id1)dva.push(vala[i]);
			o2.value = dva.join(',');
		}
	}
}

js.fileshowclick=function(id1,fid){
	var d = this.fileshowarr[id1];
	var s = '<img src="'+d.src+'" height="20" width="20" align="absmiddle">&nbsp;'+d.filename+'('+d.filesizecn+')<br><a onclick="js.fileopt('+d.id+',0)" href="javascript:;">预览</a>&nbsp;&nbsp;<a onclick="js.fileopt('+d.id+',1)" href="javascript:;">下载</a>';
	if(get(''+fid+'_view_divadd'))s+='&nbsp;&nbsp;<a onclick="js.fileshowdel(false,'+d.id+',\''+fid+'\')" href="javascript:;">删除</a>';
	js.alert(s);
	$('#amain_alert').click(function(){$(this).remove()});
}

/**
*	弹出公共上传
*/
js.showuploadfile = function(na,utype, funs){
	var fid = 'uploadfilebase';
	if(!utype)utype='';
	var str = '<div onclick="js.uploadfile(\''+fid+'\',\'base\',\''+utype+'\',false)" style="border:dashed 1px #cccccc" id="'+fid+'_view_divadd" class="upload_items"><img class="imgs" src="'+FILE_URL+'images/jia.png"></div>';
	str+='<input style="display:none" onchange="js.uploadchange(this,\''+fid+'\',\'base\')" type="file" multiple="multiple" id="'+fid+'_changeid"><input type="hidden" id="'+fid+'_id">';
	var str1= '可选择任何文件类型';
	if(utype)str1='可选'+utype+'类型的文件';
	var _w = 500;
	if(ismobile==1)_w = winWb()-20;
	js.tanbody('uploadfile',na,_w,280,{
		html:'<div align="left" style="padding:10px;height:300px;overflow:auto;"><div style="display:inline-block" id="'+fid+'_view">'+str+'</div><div style="color:#cccccc">'+str1+'，可将文件拖动到这里空白区域里</div></div>',
		btn:[{text:'确定'}]
	});
	var bool = false;
	$('#uploadfile_btn0').click(function(){
		if(!bool){
			js.uploadstart('base',-1, function(){
				get('uploadfile_btn0').innerHTML='上传完成确定添加';
				bool = true;
			});
		}else{
			var sid = get('uploadfilebase_id').value;
			js.tanclose('uploadfile');
			funs(sid);
		}
	});
	var bos = js.uploadfile(fid,'base', utype, true,{quality:1,initpdbool:false}); //初始化
	if(bos)bos.clear();
	
	document.ondragover=function(e){e.preventDefault();};
	document.ondrop=function(e){e.preventDefault();};
	get('uploadfile_main').addEventListener('drop', function(e) {
		var files = e.dataTransfer;
		js.uploadfila['base'][fid].change(files);
	}, false);
}